package dialer.ws.base.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import dialer.ws.base.dao.GenericDAO;
import dialer.ws.base.exception.EntityException;
import dialer.ws.base.exception.EntityExistException;
import dialer.ws.base.exception.EntityNoFoundException;
import dialer.ws.base.query.NamedQueryParameter;
import dialer.ws.base.query.QueryParameter;
import dialer.ws.base.service.GenericService;

@Transactional
@Service
public class GenericServiceImpl<T, PK extends Serializable> implements GenericService<T, PK> {

	protected final Log log = LogFactory.getLog(getClass());

	protected GenericDAO<T, PK> dao;

	public GenericServiceImpl() {

	}

	public GenericServiceImpl(final GenericDAO<T, PK> genericDAO) {
		log.info("GenericDAO " + genericDAO);
		this.dao = genericDAO;
	}

	@Override
	public List<T> findAll() {
		List<T> result = null;
		try {
			result = dao.findAll();
		} catch (EntityException e) {
			e.printStackTrace();
		}
		return result == null ? new ArrayList<T>(0) : result;
	}

	@Override
	public List<T> findByNamedQuery(String queryName) {
		List<T> result = null;
		try {
			result = dao.findByNamedQuery(queryName);
		} catch (EntityException e) {
			e.printStackTrace();
		}
		return result == null ? new ArrayList<T>(0) : result;
	}

	@Override
	public List<T> findByNamedQueryNamedParameter(String queryName, NamedQueryParameter... args) {
		List<T> result = null;
		try {
			result = dao.findByNamedQueryNamedParameter(queryName, args);
		} catch (EntityException e) {
			e.printStackTrace();
		}
		return result == null ? new ArrayList<T>(0) : result;
	}

	@Override
	public List<T> findByNamedQueryPositionalParameter(String queryName, QueryParameter... args) {
		List<T> result = null;
		try {
			result = dao.findByNamedQueryPositionalParameter(queryName, args);
		} catch (EntityException e) {
			e.printStackTrace();
		}
		return result == null ? new ArrayList<T>(0) : result;
	}

	@Override
	public List<T> findNamedParameter(String query, NamedQueryParameter... args) {
		List<T> result = null;
		try {
			result = dao.findNamedParameter(query, args);
		} catch (EntityException e) {
			e.printStackTrace();
		}
		return result == null ? new ArrayList<T>(0) : result;
	}

	@Override
	public List<T> findPositionalParameter(String query, QueryParameter... args) {
		List<T> result = null;
		try {
			result = dao.findPositionalParameter(query, args);
		} catch (EntityException e) {
			e.printStackTrace();
		}
		return result == null ? new ArrayList<T>(0) : result;
	}

	@Override
	public Object findByNamedQueryNamedParameterReturnObject(String queryName, NamedQueryParameter... args) {
		Object result = null;
		try {
			result = dao.findByNamedQueryNamedParameterReturnObject(queryName, args);
		} catch (EntityException e) {
			e.printStackTrace();
		}
		return result == null ? null : result;
	}

	@Override
	public T findByPk(PK pk) {
		T result = null;
		try {
			result = dao.findByPk(pk);
		} catch (EntityNoFoundException e) {
			e.printStackTrace();
		}
		return result == null ? null : result;
	}

	@Override
	public T findInstanceByNamedQuery(String queryName) {
		T result = null;
		try {
			result = dao.findInstanceByNamedQuery(queryName);
		} catch (EntityException e) {
			e.printStackTrace();
		}
		return result == null ? null : result;
	}

	@Override
	public T findInstanceByNamedQueryNamedParameter(String queryName, NamedQueryParameter... args) {
		T result = null;
		try {
			result = dao.findInstanceByNamedQueryNamedParameter(queryName, args);
		} catch (EntityNoFoundException e) {
			e.printStackTrace();
		} catch (EntityException e) {
			e.printStackTrace();
		}
		return result == null ? null : result;
	}

	@Override
	public T findInstanceByNamedQueryPositionalParameter(String queryName, QueryParameter... args) {
		T result = null;
		try {
			result = dao.findInstanceByNamedQueryPositionalParameter(queryName, args);
		} catch (EntityException e) {
			e.printStackTrace();
		}
		return result == null ? null : result;
	}

	@Override
	public void flush() throws EntityException {
		// TODO Auto-generated method stub

	}

	@Override
	public Object merge(Object object) throws EntityException {
		return dao.merge(object);
	}

	@Override
	public Object persist(Object object) throws EntityExistException, EntityException {
		return dao.persist(object);
	}

	@Override
	public Object saveOrUpdate(Object object) throws EntityExistException, EntityException {
		return dao.saveOrUpdate(object);
	}

	@Override
	public void remove(Object object) throws EntityException {
		dao.remove(object);
	}

	@Override
	public void clear() throws EntityException {
		dao.clear();
	}

}
